#!/bin/bash

export PROJECT_BUILD_PATH="$(pwd)/build"
export VSCODE_PATH="$(pwd)/../.vscode"

BUILD_DIR="build"
SOURCE_DIR="."

DEVICE_TYPE="IMX6ULL"

if [ "$#" -eq 1 ]; then
    BUILD_TYPE=$1
else
    BUILD_TYPE="debug"
fi

# 将 BUILD_TYPE 转为小写以进行比较
BUILD_TYPE_LOWER=$(echo "$BUILD_TYPE" | tr '[:upper:]' '[:lower:]')

# 检查并创建 build 目录
if [ ! -d "$BUILD_DIR" ]; then
    mkdir "$BUILD_DIR"
    echo "Created build directory: $(pwd)/${BUILD_DIR}"
fi

if [ "$BUILD_TYPE_LOWER" == "debug" ]; then
    cmake -S "$SOURCE_DIR" -B ${BUILD_DIR} -DCMAKE_BUILD_TYPE=Debug -DBUILD_PLATFORM=${DEVICE_TYPE}
elif [ "$BUILD_TYPE_LOWER" == "release" ]; then
    cmake -S "$SOURCE_DIR" -B ${BUILD_DIR} -DCMAKE_BUILD_TYPE=Release -DBUILD_PLATFORM=${DEVICE_TYPE}
else
    echo "Error: Unsupported build type '$BUILD_TYPE'. Supported: debug, release."
    exit 1
fi

cmake --build build